package com.sjtuy.galtxtreader.gal;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjtuy.galtxtreader.R;

public class SLListItemView extends LinearLayout {
    public static final int SAVE_MODE=0;
    public static final int LODE_MODE=1;
    private int mode;
    private int lSocket;
    private int rSocket;
    private SLDoer slDoer;

    private RelativeLayout bookMarkLLayout;
    private RelativeLayout bookMarkRLayout;
    private TextView lChapterNameView;
    private TextView rChapterNameView;
    private TextView lThumbnailView;
    private TextView rThumbnailView;
    public SLListItemView(Context context, final int mode, final int position, final SLDoer slDoer, TextProvider textProvider) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.listitem_sl,this);
        bookMarkLLayout=findViewById(R.id.bookMarkL);
        bookMarkRLayout=findViewById(R.id.bookMarkR);
        lChapterNameView=findViewById(R.id.bookMarkChapterNameL);
        rChapterNameView=findViewById(R.id.bookMarkChapterNameR);
        lThumbnailView=findViewById(R.id.bookMarkThumbnailL);
        rThumbnailView=findViewById(R.id.bookMarkThumbnailR);
        this.mode=mode;
        this.slDoer = slDoer;
        lSocket=position*2+1;
        rSocket=position*2+2;

        bookMarkLLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mode==SAVE_MODE){
                    slDoer.save(lSocket);
                }else if(mode==LODE_MODE){
                    slDoer.load(lSocket);
                }
            }
        });
        bookMarkRLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mode==SAVE_MODE){
                    slDoer.save(rSocket);
                }else if(mode==LODE_MODE){
                    slDoer.load(rSocket);
                }
            }
        });
        lChapterNameView.setText(textProvider.getChapterBySocket(lSocket));
        rChapterNameView.setText(textProvider.getChapterBySocket(rSocket));
        lThumbnailView.setText(textProvider.getDialogBySocket(lSocket));
        rThumbnailView.setText(textProvider.getDialogBySocket(rSocket));
    }


    public interface SLDoer{
        public void save(int socket);
        public void load(int socket);
    }
}
